﻿using EMS.system.IEntityAppService.MasterData.SupplierManagementIService;
using EMS.system.IEntityAppService.MasterData.SupplierManagementIService.SupplierManagementDto;
using Humanizer;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace EMS.system.Web.Controllers.MasterData.Supplierss
{
    public class SuppliersController : Controller
    {
        private readonly ISupplierManagementAppServices _supplierManagementAppServices;

        public SuppliersController(ISupplierManagementAppServices supplierManagementAppServices)
        {
            _supplierManagementAppServices = supplierManagementAppServices;
        }

        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 供应商管理
        /// </summary>
        /// <returns></returns>
        public IActionResult Suppliers()
        {
            return View();
        }

        /// <summary>
        /// 供应商管理显示
        /// </summary>
        /// <param name="SupplierManagementCode"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSupplierManagement(string? SupplierManagementCode, int page, int limit)
        {
            var (units, total) = _supplierManagementAppServices.GetSupplierManagement(SupplierManagementCode, page, limit);
            return Json(new { code = 0, data = units, total = total });
        }
        /// <summary>
        /// 供应商管理新增
        /// </summary>
        /// <returns></returns>
        public IActionResult AddSuppliers()
        {
            return View();
        }
        /// <summary>
        /// 供应商管理新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Add(SelectSupplierManagementDto dto)
        {
            var list = _supplierManagementAppServices.InsertSupplierManagement(dto);
            return Json(new { code = 1, msg = "新增成功" });
        }
        /// <summary>
        /// 供应商管理修改
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateSuppliers(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 供应商管理反填
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> SelectUpdate(int id)
        {
            var list = await _supplierManagementAppServices.GetUpdate(id);
            return Json(new { data = list, msg = "反填成功" });
        }
        /// <summary>
        /// 供应商管理修改
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Task<int> Update(SelectSupplierManagementDto dto)
        {
            var list = _supplierManagementAppServices.UpdateSupplierManagement(dto);
            return Task.FromResult(1);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<int> Delete(int id)
        {
            var list = _supplierManagementAppServices.DeleteSupplierManagement(id);
            return Task.FromResult(1);
        }
    }
}
